package com.example.myapplication;

import com.example.baselib.utils.PermissionUtils;
import com.example.baselib.utils.log.LogUtils;
import com.example.myapplication.data.EConstants;
import com.example.myapplication.slice.AvatarAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;

public class AvatarAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(AvatarAbilitySlice.class.getName());
        String[] ps = {"ohos.permission.CAMERA"};
        if (!PermissionUtils.hasPermission(this, ps)) {
            requestPermissionsFromUser(ps, EConstants.REQUEST_CODE_PERMISSIONS_CAMERA);
        }
    }



    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == EConstants.REQUEST_CODE_PERMISSIONS_CAMERA) {
            LogUtils.i("onRequestPermissionsFromUserResult : %s", requestCode);
            if (grantResults.length > 0 && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {

            } else {
                // todo 权限被拒绝
                new ToastDialog(this)
                        .setDuration(2500)
                        .setText("拒绝授权相机功能将无法使用,请手动开启")
                        .show();
            }
        }
    }
}
